package gui;

import interfaces.Command;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import org.apache.log4j.*;

import mediator.Mediator;
import javax.swing.JButton;
import java.awt.Color;

public class Main implements ActionListener {

	public JFrame frmAuctionHouse;
	public JLabel lblUserType;
	
	Mediator med;
	public JTable table;
	public JScrollPane scrollPane;
	public DefaultTableModel dtm;
	public JPopupMenu popMenuBuyer1;
	public JPopupMenu popMenuBuyer2;
	public JPopupMenu popMenuSeller;
	public JTextField notificationsTxt;

	public static Logger logger = Logger.getLogger(Main.class);
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		PropertyConfigurator.configure("log4j.properties");
		logger.info("Application started.");
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main window = new Main();
					window.frmAuctionHouse.setVisible(false);
				} catch (Exception e) {
					logger.fatal("Fatal error in main window.");
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Main() {
		initialize();
		
		med = new Mediator();
		med.registerMainWindow(this);
		frmAuctionHouse.getContentPane().setLayout(null);
		
		lblUserType = new JLabel("New label");
		lblUserType.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblUserType.setBounds(10, 11, 220, 25);
		frmAuctionHouse.getContentPane().add(lblUserType);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(10, 47, 808, 13);
		frmAuctionHouse.getContentPane().add(separator);
		
		// Create Popup Menus
		popMenuBuyer1 = new JPopupMenu();
		PopupItemLaunchOffer plo = new PopupItemLaunchOffer(this, med);
		PopupItemDropOffer pdo = new PopupItemDropOffer(this, med);
		popMenuBuyer1.add(plo);
		popMenuBuyer1.add(pdo);
		
		popMenuBuyer2 = new JPopupMenu();
		PopupItemAcceptOffer pao = new PopupItemAcceptOffer(this, med);
		PopupItemRefuseOffer pro = new PopupItemRefuseOffer(this, med);
		popMenuBuyer2.add(pao);
		popMenuBuyer2.add(pro);
		
		popMenuSeller = new JPopupMenu();
		PopupItemMakeOffer pmo = new PopupItemMakeOffer(this, med);
		PopupItemDropAuction pda = new PopupItemDropAuction(this, med); 
		popMenuSeller.add(pmo);
		popMenuSeller.add(pda);
		
		LogoutBtn btnLogout = new LogoutBtn(this, med);
		btnLogout.setFont(new Font("Tahoma", Font.ITALIC, 12));
		btnLogout.setBackground(new Color(250, 128, 114));
		btnLogout.setForeground(Color.BLACK);
		btnLogout.setBounds(682, 10, 109, 30);
		frmAuctionHouse.getContentPane().add(btnLogout);

		RefreshBtn btnRefresh = new RefreshBtn(this, med);
		btnRefresh.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnRefresh.setBounds(546, 10, 109, 30);
		frmAuctionHouse.getContentPane().add(btnRefresh);
		
		// Initiate the Table Model
		dtm = new DefaultTableModel() {
			@Override
		    public boolean isCellEditable(int row, int column) {
		       //all cells false
		       return false;
		    }
		};
		
		table = new JTable(dtm);
		table.setRowSelectionAllowed(true);
		table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);
		
        // Add table to Scroll Pane
		scrollPane = new JScrollPane(table);
		scrollPane.setBounds(32, 85, 759, 435);
		
		// Add Scroll Pane to current window
		frmAuctionHouse.getContentPane().add(scrollPane); 
		
		notificationsTxt = new JTextField();
		notificationsTxt.setBackground(SystemColor.inactiveCaptionBorder);
		notificationsTxt.setEditable(false);
		notificationsTxt.setBounds(128, 529, 663, 20);
		frmAuctionHouse.getContentPane().add(notificationsTxt);
		notificationsTxt.setColumns(10);
		
		JLabel lblStatusBar = new JLabel("Notifications");
		lblStatusBar.setFont(new Font("Tahoma", Font.ITALIC, 12));
		lblStatusBar.setBounds(32, 531, 86, 14);
		frmAuctionHouse.getContentPane().add(lblStatusBar);
		
		// Instantiate Mouse App
		MouseApp mapp = new MouseApp(med);
		table.addMouseListener(mapp);
		
		LoginDialog logDiag = new LoginDialog(med);
		logDiag.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		logDiag.setVisible(true);
		
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmAuctionHouse = new JFrame();
		frmAuctionHouse.setTitle("Auction House");
		frmAuctionHouse.setBounds(100, 100, 844, 598);
		frmAuctionHouse.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		((Command)e.getSource()).execute();
	}
}
